package com.business.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.business.base.tools.key.PKConfContext;
import com.business.base.tools.key.PKIDTools;
import com.business.system.dao.TabCarOwnerInfoMapper;
import com.business.system.dao.TabRepairCldjMapper;
import com.business.system.dao.TabRepairFlowMapper;
import com.business.system.pojo.TabCarOwnerInfo;
import com.business.system.pojo.TabRepairCldj;
import com.business.system.pojo.TabRepairCldjExample;
import com.business.system.pojo.TabRepairFlow;
import com.business.system.service.ITabRapairCLDJService;
import com.utility.Page;

@Service
public class TabRepairCLDJServiceImpl implements ITabRapairCLDJService {

	@Autowired
	private TabRepairCldjMapper repairCldjMapper;
	@Autowired
	private TabCarOwnerInfoMapper carOwnerInfoMapper;
	@Autowired
	private TabRepairFlowMapper repairFlowMapper;

	@Override
	public int delete(String id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.repairCldjMapper.deleteByPrimaryKey(id);
	}

	@Override
	public TabRepairCldj findById(String id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.repairCldjMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<TabRepairCldj> findList(TabRepairCldj entity)
			throws RuntimeException {
		// TODO Auto-generated method stub
		TabRepairCldjExample example = new TabRepairCldjExample();
		example.createCriteria().andFlowIdEqualTo(entity.getFlowId());
		return this.repairCldjMapper.selectByExample(example);
	}

	@Override
	public Page<TabRepairCldj> findListPage(TabRepairCldj entity, int pageNo,
			int pageSize) throws RuntimeException {
		// TODO Auto-generated method stub
		Page<TabRepairCldj> page = new Page<TabRepairCldj>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("flowId", entity.getFlowId());
		page.setParams(map);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);

		List<TabRepairCldj> list = this.repairCldjMapper.findListPage(page);
		page.setResults(list);
		return page;
	}

	@Override
	public int insert(TabRepairCldj entity) throws RuntimeException {
		// TODO Auto-generated method stub
		int res = 0;

		/**
		 * <code>
		 * 同步车主信息
		 */
		String carOwnerInfoId = "";
		TabCarOwnerInfo carOwnerInfo = new TabCarOwnerInfo();
		carOwnerInfo.setCarBrands(entity.getCarBrands());
		carOwnerInfo.setCarChassisNo(entity.getCarChassisNo());
		carOwnerInfo.setCarColor(entity.getCarColor());
		carOwnerInfo.setCarEngineNo(entity.getCarEngineNo());
		carOwnerInfo.setCarModel(entity.getCarModel());
		carOwnerInfo.setCarTagNumber(entity.getCarTagNumber());
		carOwnerInfo.setCarTagType(entity.getCarTagType());
		carOwnerInfo.setOwnerContact(entity.getOwnerContact());
		carOwnerInfo.setOwnerMobile(entity.getOwnerMobile());
		carOwnerInfo.setOwnerName(entity.getOwnerName());
		carOwnerInfo.setOwnerTelephone(entity.getOwnerTelephone());
		carOwnerInfo.setCarDriverLength(entity.getCarDriverLength());
		carOwnerInfo.setCarBuyTime(entity.getCarBuyTime());
		carOwnerInfo.setCarAuditTime(entity.getCarAuditTime());
		if (entity.getCarOwnerInfoId() == null
				|| "".equals(entity.getCarOwnerInfoId())) {

			carOwnerInfoId = PKIDTools.getSerialID(PKConfContext.NO_Len_Four,
					PKConfContext.CarOwnerInfoID_DataPath,
					PKConfContext.CarOwnerInfoID_Suffix);
			carOwnerInfo.setId(carOwnerInfoId);

			carOwnerInfo.setCreateTime(new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss").format(new Date()));
			this.carOwnerInfoMapper.insert(carOwnerInfo);
		} else {
			carOwnerInfoId = entity.getCarOwnerInfoId();
			carOwnerInfo.setId(carOwnerInfoId);
			this.carOwnerInfoMapper.updateByPrimaryKeySelective(carOwnerInfo);
		}

		String v_flow_id = "";
		if (entity.getFlowId() != null && !"".equals(entity.getFlowId())) {
			v_flow_id = entity.getFlowId();
			// 维修流程
			TabRepairFlow repairFlow = new TabRepairFlow();
			repairFlow.setId(v_flow_id);
			repairFlow.setFlowStep("2");
			repairFlow.setFlowStepState("1");
			repairFlow.setCarOwnerInfoId(carOwnerInfoId);
			// 维修类型
			repairFlow.setWxlx(entity.getWxlx());
			// 客户陈述
			repairFlow.setCzzgms(entity.getCzzgms());
			// 受理时间
			// repairFlow.setSlsj(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
			// .format(new Date()));
			// 服务顾问
			repairFlow.setFwgw(entity.getFwgw());
			this.repairFlowMapper.updateByPrimaryKeySelective(repairFlow);
		} else {
			// 维修流程单号
			String flow_id = PKIDTools.getSerialID(PKConfContext.NO_Len_Four,
					PKConfContext.RepairFlowID_DataPath,
					PKConfContext.RepairFlowID_Suffix);
			// 维修流程
			TabRepairFlow repairFlow = new TabRepairFlow();
			repairFlow.setId(flow_id);
			repairFlow.setFlowStep("2");
			repairFlow.setFlowStepState("1");
			repairFlow.setCarOwnerInfoId(carOwnerInfoId);
			// 维修类型
			repairFlow.setWxlx(entity.getWxlx());
			// 客户陈述
			repairFlow.setCzzgms(entity.getCzzgms());
			// 受理时间
			repairFlow.setSlsj(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.format(new Date()));
			// 服务顾问
			repairFlow.setFwgw(entity.getFwgw());
			this.repairFlowMapper.insert(repairFlow);

			v_flow_id = flow_id;
		}

		// 车主信息号
		entity.setCarOwnerInfoId(carOwnerInfoId);
		entity.setFlowId(v_flow_id);
		entity.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date()));
		// 登记单号
		String dj_no = PKIDTools.getSerialID(PKConfContext.NO_Len_Four,
				PKConfContext.RepairCLDJID_DataPath,
				PKConfContext.RepairCLDJID_Suffix);
		entity.setId(dj_no);
		res = this.repairCldjMapper.insert(entity);
		return res;
	}

	@Override
	public int update(TabRepairCldj entity) throws RuntimeException {

		TabCarOwnerInfo carOwnerInfo = new TabCarOwnerInfo();
		carOwnerInfo.setCarBrands(entity.getCarBrands());
		carOwnerInfo.setCarChassisNo(entity.getCarChassisNo());
		carOwnerInfo.setCarColor(entity.getCarColor());
		carOwnerInfo.setCarEngineNo(entity.getCarEngineNo());
		carOwnerInfo.setCarModel(entity.getCarModel());
		carOwnerInfo.setCarTagNumber(entity.getCarTagNumber());
		carOwnerInfo.setCarTagType(entity.getCarTagType());
		carOwnerInfo.setOwnerContact(entity.getOwnerContact());
		carOwnerInfo.setOwnerMobile(entity.getOwnerMobile());
		carOwnerInfo.setOwnerName(entity.getOwnerName());
		carOwnerInfo.setOwnerTelephone(entity.getOwnerTelephone());
		carOwnerInfo.setCarDriverLength(entity.getCarDriverLength());
		carOwnerInfo.setCarBuyTime(entity.getCarBuyTime());
		carOwnerInfo.setCarAuditTime(entity.getCarAuditTime());

		if (entity.getCarOwnerInfoId() != null
				&& !"".equals(entity.getCarOwnerInfoId())) {
			String carOwnerInfoId = entity.getCarOwnerInfoId();
			carOwnerInfo.setId(carOwnerInfoId);
			this.carOwnerInfoMapper.updateByPrimaryKeySelective(carOwnerInfo);
		}
		if (entity.getFlowId() != null && !"".equals(entity.getFlowId())) {
			// 维修流程
			TabRepairFlow repairFlow = new TabRepairFlow();
			repairFlow.setId(entity.getFlowId());
			// repairFlow.setFlowStep("2");
			// repairFlow.setFlowStepState("2");
			repairFlow.setCarOwnerInfoId(entity.getCarOwnerInfoId());
			// 维修类型
			repairFlow.setWxlx(entity.getWxlx());
			// 客户陈述
			repairFlow.setCzzgms(entity.getCzzgms());
			// 受理时间
			// repairFlow.setSlsj(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
			// .format(new Date()));
			// 服务顾问
			repairFlow.setFwgw(entity.getFwgw());
			this.repairFlowMapper.updateByPrimaryKeySelective(repairFlow);
		}

		// TODO Auto-generated method stub
		return this.repairCldjMapper.updateByPrimaryKeySelective(entity);
	}
}
